package com.fast.alden.common.entity.excel;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.fast.alden.data.model.SysUser;
import lombok.Data;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Data
@ColumnWidth(20)
public class UserExcelEntity {
    /**
     * ID，唯一标识，使用主键自增策略
     */
    @ColumnWidth(10)
    @ExcelProperty("ID")
    private Long id;

    /**
     * 用户名
     */
    @ExcelProperty("用户名")
    private String username;

    /**
     * 密码，只可反序列化不可序列化，即返回给前端的json串不含此属性
     */
    @ExcelProperty("密码")
    private String password;

    /**
     * 昵称
     */
    @ExcelProperty("昵称")
    private String nickname;

    /**
     * 真实姓名
     */
    @ExcelProperty("真实姓名")
    private String realName;

    /**
     * 邮箱
     */
    @ExcelProperty("邮箱")
    private String email;

    /**
     * 电话
     */
    @ExcelProperty("电话")
    private String phone;

    /**
     * 生日
     */
    @ExcelProperty("生日")
    private LocalDate birthday;

    /**
     * 个人介绍
     */
    @ColumnWidth(30)
    @ExcelProperty("个人介绍")
    private String introduce;

    /**
     * 头像，以base64编码字符串存储
     */
    @ColumnWidth(30)
    @ExcelProperty("头像")
    private String avatar;

    /**
     * 创建时间
     */
    @ExcelProperty("创建时间")
    private LocalDateTime createdTime;

    /**
     * 最后修改时间
     */
    @ExcelProperty("最后修改时间")
    private LocalDateTime lastModifiedTime;

    /**
     * 创建人ID
     */
    @ExcelProperty("创建人ID")
    private Long creatorId;

    /**
     * 创建人昵称
     */
    @ExcelProperty("创建人昵称")
    private String creatorName;

    /**
     * 最后修改人对象
     */
    @ExcelProperty("最后修改人ID")
    private Long lastModifierId;

    /**
     * 创建人昵称
     */
    @ExcelProperty("最后修改人昵称")
    private String lastModifierName;

    @ExcelProperty("状态，1-正常，0-停用")
    private Integer status;

    public static UserExcelEntity loadFromModel(SysUser user) {
        UserExcelEntity excelEntity = new UserExcelEntity();

        excelEntity.setId(user.getId());
        excelEntity.setUsername(user.getUsername());
        excelEntity.setPassword(user.getPassword());
        excelEntity.setNickname(user.getNickname());
        excelEntity.setRealName(user.getRealName());
        excelEntity.setEmail(user.getEmail());
        excelEntity.setPhone(user.getPhone());
        excelEntity.setBirthday(user.getBirthday());
        excelEntity.setIntroduce(user.getIntroduce());
        excelEntity.setAvatar(user.getAvatar());
        excelEntity.setStatus(user.getStatus());
        excelEntity.setCreatedTime(user.getCreatedTime());
        excelEntity.setLastModifiedTime(user.getLastModifiedTime());
        excelEntity.setCreatorId(user.getCreatorId());
        excelEntity.setCreatorName(user.getCreatorName());
        excelEntity.setLastModifierId(user.getLastModifierId());
        excelEntity.setLastModifierName(user.getLastModifierName());

        return excelEntity;
    }

    public static SysUser convertToModel(UserExcelEntity excelEntity) {
        SysUser user = new SysUser();

        user.setId(excelEntity.getId());
        user.setUsername(excelEntity.getUsername());
        user.setPassword(excelEntity.getPassword());
        user.setNickname(excelEntity.getNickname());
        user.setRealName(excelEntity.getRealName());
        user.setEmail(excelEntity.getEmail());
        user.setPhone(excelEntity.getPhone());
        user.setBirthday(excelEntity.getBirthday());
        user.setIntroduce(excelEntity.getIntroduce());
        user.setAvatar(excelEntity.getAvatar());
        user.setStatus(excelEntity.getStatus());
        user.setCreatedTime(excelEntity.getCreatedTime());
        user.setLastModifiedTime(excelEntity.getLastModifiedTime());
        user.setCreatorId(excelEntity.getCreatorId());
        user.setLastModifierId(excelEntity.getLastModifierId());

        return user;
    }
}
